<!DOCTYPE html>
<html>
	<body>
		<p id="demo"></p>

		<script src="./js/lodash-4.17.21.js"></script>
		<script type="module">
			var cars = ["BMW", "Volvo", "porsche", "Ford"];
			var i = 2;
			var len = cars.length;
			var text = "";
			for (; i < len; i++) {
				text += cars[i] + "<br>";
			}
			document.getElementById("demo").innerHTML = text; // porsche, Ford

			let arr = [{
					id: 1,
					name: 'a'
				},
				{
					id: 2,
					name: 'b'
				},
				{
					id: 3,
					name: 'c'
				},
				{
					id: 1,
					name: 'aa'
				},
				{
					id: 2,
					name: 'bb'
				}
			]
			let objTemp = {},
				newArr = [];
			for (let i in arr) {
				var v = arr[i];
				if (!objTemp[v.id]) {
					newArr.push({
						id: v.id,
						data: [v]
					})
					objTemp[v.id] = v
				} else {
					// for (let j in newArr) {
					// 	var v2 = newArr[j];
					// 	if (v2.id == v.id) {
					// 		v2.data.push(v);
					// 		break
					// 	}
					// }
					var find = newArr.find(function(v2, k2) {
						return v2.id == v.id
					})
					if (find) {
						find.data.push(v)
					}
				}
			}
			console.log(newArr, 'newArr')
			/*
			  [
					{
			  		id: 1,
						data: [
							{
								id: 1,
								name: 'a'
							},
							{
								id: 1,
								name: 'aa'
							}
						]
			  	},
			  	{
			  		id: 2,
			  		data: [
			  			{
			  				id: 2,
			  				name: 'b'
			  			},
			  			{
			  				id: 2,
			  				name: 'bb'
			  			}
			  		]
			  	},
			  	{
			  		id: 3,
						data: [
							{
								id: 3,
								name: 'c'
							}
						]
			  	}
			  ]
			*/
		  
			// 循环引用
			const ab = {
				a: 1,
				b: 1
			}
			ab.fn = ab
			console.log(ab, 'ab')
			
			// 去重
			function removeDuplicates(data, key) {
				var obj = {},
					arr = [],
					isData = data && data.length
				if (isData) {
					arr = data.reduce(function(pre, cur) {
						obj[cur[key]] ? '' : obj[cur[key]] = true && pre.push(cur)
						return pre
					}, [])
				}
				return arr
			}

			import {
				orgTree,
				testData,
				testData2
			} from "./js/tree.js"
			function oneToMany(data) {
				// debugger
				let id = 'id';
				let name = 'name';
				let pId = 'parentId';
				let tree = [];
				let temp = {};
				for (let i in data) {
					temp[data[i][id]] = data[i]
				}
				console.log(temp, 'tempObj')
				for (let i = 0; i < data.length; i++) {
					const val = data[i]
					const vPId = val[pId]
					if (temp[vPId] && val[id] != vPId) {
						if (!temp[vPId]['children']) {
							temp[vPId]['children'] = []
						}
						val['text'] = val[name];
						temp[vPId]['children'].push(val)
					} else {
						val['text'] = val[name];
						tree.push(val)
					}
				}
				return tree
			}
			function oneToManyLodash(data, id, pId) {
				var tree = [],
					id = id || 'id',
					pId = pId || 'parentId',
					isData = data && data.length,
					// cD = _.cloneDeep(data),
					// pArr = _.groupBy(cD, pId)
					pArr = _.groupBy(data, pId)
				// debugger
				console.log(pArr, 'pArr')
				if (isData) {
					// _.each(pArr, function(v, k) {
					// 	var pF = _.find(cD, function(v2) {
					// 	  return v2[id] == k
					// 	})
					// 	// var pF = _.find(cD, id, k)
					// 	if (!pF) {
					// 		tree = tree.concat(v)
					// 	} else {
					// 		pF.children = _.sortBy(v, id)
					// 	}
					// })
					for (var k in pArr) {
						var v = pArr[k]
						// var pF = cD.find(function(v2) {
						var pF = data.find(function(v2) {
							return v2[id] == k && v2[id] != v2[pId]
						})
						console.log(pF, 'pF')
						if (!pF) {
							tree = tree.concat(v)
							tree.sort(function(a, b) {
								return (a && a.id) - (b && b.id)
							})
						} else {
							pF.children = v.sort(function(a, b) {
								return (a && a.id) - (b && b.id)
							})
						}
					}
				}
				return tree
			}
			console.time("运算时间")
			// const newTree = oneToMany(testData2)
			const newTree = oneToManyLodash(testData2)
			console.timeEnd("运算时间")
			console.log(newTree, 'newTree')
		</script>
	</body>
</html>